/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"

void Tx_mode();
static volatile unsigned char rx_buf[10] ={0x22};
static volatile unsigned char cnt = 0;

bit Rx_OK = 0;


/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay (int cnt )
{
	unsigned int i , j ;
	
	for(j=0 ; j<cnt ; j++)
	for( i = 0 ; i < 200 ; i++ ); 
	
}


/**
 * @brief       UARTISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void UARTISR (void) interrupt ISR_UART
{
	unsigned char int_stat = UART->iir_fcr;
	
	if ((int_stat & 0x04) == 0x04)			//  Wait for receive data
	
	rx_buf[0] = UART->bufdat_dll;  //   Read Rx buffer 
	
	INTC->int_gmask = 0x00;  //  Disable global  interrupt
	
	Tx_mode(); //  Transfer data form Rx buffer 
	
	INTC->int_gmask = 0x01;   //	Enable global  interrupt
	
	//Rx_OK = 1;
}


/**
 * @brief       UARTRXConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void Rx_mode()
{
	/* set line control */
	UART->line_ctrl = 0x03;            // DLAB = 0, PEN = 0, STB = 0, WLS = 3
	
	/* set fifo control register */
	UART->iir_fcr = 0x01;              // RFIFOT = 0, TFIFOT = 0, DMAE = 0, TFIFORST = 0, RFIFORST = 0, FIFOE = 0
	
	/* enable interrupt */
	UART->ier_dlm = 0x01;              // EMSI = 0, ELSI = 0, ETHEI = 0, ERBI = 1
	INTC->int_gmask = 0x01;            // Enable global interrupt
}


void Tx_mode()
{
	
	/* set line control */
	UART->line_ctrl = 0x03;            
	
	/* set fifo control register */
	UART->iir_fcr = 0x07;              
	
	/* Loop this section to transfer more data */
	UART->bufdat_dll = rx_buf[0];
 		while((UART->line_stat & 0x20) == 0x00){}  //  Wait for Tx done
}


/**
 * @brief       IOConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void IOConfig()
{
	
	/* enable P0.6(RX), P0.7(TX) function */
	Set_PA6_Rx();
	Set_PA7_Tx();
	
	/* Set PA6 input, PA.7 output */
 	PORT_SetPAPushPull(BIT7);
	PORT_SetPAInput(BIT6);
	
	/* Set PD1. ,PD2.,PD3 output */
	PORT_SetPDPushPull(BIT1 | BIT2 | BIT3 );

}


/**
 * @brief       CLKConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CLKConfig()
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();
	
	/* enable UART clock */
	En_UART_clk();
}

void UART_config()
{
		CLKConfig();
		IOConfig();
	
		Set_BaudRate_115200(); /* set baud rate */
	
}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main (void)
{
	UART_config();
	Rx_mode();	

	PD1 = 0; // LED_G1 On
	PD3= 0;  // LED_G2  On
	
			while(1)
			{
					PD2 =  ~PD2 ; // twinkling LED_R
					delay(500); // rough 0.5s
				
			}
}

